function deepClone(obj){
    if(!obj) return obj;
    let objCopy = null;
    if(typeof obj !== 'object') {
        objCopy = obj;
    }else{
        objCopy = Array.isArray(obj) ? [] : {};
        for(let key in obj){
            if(obj.hasOwnProperty(key)){
                objCopy[key] = deepClone(obj[key]);
            }
        }
    }
    return objCopy;
}


const getAllSubStr = (str) => {
    if(!str || str.length === 0) return str;
    let result = [];
    for(let i = 0; i < str.length; i++){
        for(let j = i+1; j <= str.length; j++){
            result.push(str.substring(i,j));
        }
    }
    return result;
}


const findContentChildren = (childrenList, cookiesList) => {
    let res = 0, i=0, j=0;
    let childrenLen = childrenList.length;
    let cookiesLen = cookiesList.length;
    while (i<childrenLen && j < cookiesLen){
        if(childrenList[i] < cookiesList[i]){
            res+=1;
            i++;
            j++
        }else{
            j++;
        }
    }
    return res;
}

const calc_multi = (list) => {
    if(!Array.isArray(list) || list.length === 0) return false;
    let sortList = list.sort();
    let max = 0;
    let front = 0; let len = sortList.length;
    for(let i = front; i < len; i++){
        let lower= sortList[i];
        let sum = 0;
        for(let j = i; j < len; j++){
            sum += sortList[j];
        }
        let multi = lower * sum;
        if(multi > max){
            max = multi
        }else{
            continue
        }
    }
    return max;
}

const threeSum = (list) => {
    let [len, res, diff] = [list.length,0, Infinity];
    if(len > 2){
        list = list.sort();
        for(let i = 0; i < len - 2; i++){
            let left = i + 1;
            let right = len - 1;
            while(left < right){
                let sum = list[i] + list[left] + list[right];
                if(Math.abs(sum - target) < diff){
                    res = sum;
                    diff = Math.abs(sum - target)
                }
                if(sum === target){
                    return res;
                }else if(sum > target){
                    right--;
                }else{
                    left++;
                }
            }
        }
    }
}

const maxArea = function(height){
    let [maxNow, start, end] = [0, 0, height.length -1];
    while(start < end){
        let width = end - start;
        let high = 0;
        if(height[start]< height[end]){
            high = height[start];
            start++;
        }else{
            high = height[end];
            end--;
        }
        if(maxNow < high * width){
            maxNow = high * width;
        }
    }
    return maxNow
}


let person = {
    age: 18
}

let newPerson = {};

Object.keys(person).forEach((key) => {
    Object.defineProperty(newPerson,key,{
        enumerable: true,
        configurable: true,
        get: function(){
            console.log("访问了属性");
            return person[key];
        },
        set: function(value){
            person[key] = value;
        }
    })
});

let proxy = new Proxy(person,{
    get(target,key){
        console.log("访问了该该属性 " + key)
        return target[key];
    },
    set(target,key,value){
        target[key] = value;
        return true;
    }
})

const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';


function resolvePromise(x, resolve, reject) {
    // 判断x是不是 MyPromise 实例对象
    if (x instanceof MyPromise) {
        // 执行 x，调用 then 方法，目的是将其状态变为 fulfilled 或者 rejected
        // x.then(value => resolve(value), reason => reject(reason))
        // 简化之后
        x.then(resolve, reject)
    } else {
        // 普通值
        resolve(x)
    }
}
class MyPromise{
    constructor(executor) {
        this.status = PENDING;
        this.value = undefined;
        this.reason = undefined;
        this.successCallback = [];
        this.failedCallback = [];

        let resolve = (value) => {
            if(this.status === PENDING){
                this.status = FULFILLED;
                this.value = value;
                this.successCallback.forEach(fn => fn());
            }
        }

        let reject = (reason) => {
            if(this.status === PENDING){
                this.status = REJECTED;
                this.reason = reason;
                this.failedCallback.forEach(fn => fn());
            }
        }

        try{
            executor(resolve,reject)
        }catch(error){
            reject(error)
        }
    }
    then(onSuccessFunc, onFailedFunc){
        const promise2 = new MyPromise((resolve,reject) => {
            if(this.status === FULFILLED){
                const x = onSuccessFunc(this.value);
                resolvePromise(x,resolve,reject);
            }else if(this.status === REJECTED){
                onFailedFunc(this.reason);
            }else if(this.status === PENDING){
                this.successCallback.push(() => onSuccessFunc(this.value))
                this.failedCallback.push(() => onFailedFunc(this.reason))
            }
        })
        return promise2;
    }
}


const promise = new MyPromise((resolve, reject) => {
    // 目前这里只处理同步的问题
    resolve('success')
})

function other () {
    return new MyPromise((resolve, reject) =>{
        resolve('other')
    })
}
promise.then(value => {
    console.log(1)
    console.log('resolve', value)
    return other()
}).then(value => {
    console.log(2)
    console.log('resolve', value)
})
